﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Runtime.InteropServices;

namespace JTicket.Printer
{
    public class c500USB
    {
        /// <summary>
        /// 说明:  挂起当前线程
        /// 返回:  无
        /// </summary>
        /// <param name="dwMilliseconds">挂起时间(毫秒)</param>
        [DllImport("kernel32")]
        public static extern void Sleep(Int32 dwMilliseconds);

        [DllImport("RC500USB.dll")]
        public static extern byte RC500USB_init();
        [DllImport("RC500USB.dll")]
        public static extern byte RC500USB_exit();
        [DllImport("RC500USB.dll")]
        public static extern byte RC500USB_buzzer(byte a, byte b,byte c,byte d);
        [DllImport("RC500USB.dll")]
        public static extern byte RC500USB_request(byte mode, ref short tagtype);
        [DllImport("RC500USB.dll")]
        public static extern byte RC500USB_anticoll(byte bcnt, ref int snr);
        [DllImport("RC500USB.dll")]
        public static extern byte RC500USB_anticoll2(byte encoll, byte bcnt, ref int snt);
        [DllImport("RC500USB.dll")]
        public static extern byte RC500USB_select(int snr, ref byte size);
        [DllImport("RC500USB.dll")]
        public static extern byte RC500USB_authentication(byte mode, byte secnr);
        [DllImport("RC500USB.dll")]
        public static extern byte RC500USB_authentication2(byte mode, byte secnr, byte keynr);
        [DllImport("RC500USB.dll")]
        public static extern byte RC500USB_authkey(byte mode, byte secnr, byte[] key);
        [DllImport("RC500USB.dll")]
        public static extern byte RC500USB_halt();
        [DllImport("RC500USB.dll")]
        public static extern byte RC500USB_read(byte addr, byte[] data);
        [DllImport("RC500USB.dll")]
        public static extern byte RC500USB_write(byte addr, byte data);
        [DllImport("RC500USB.dll")]
        public static extern byte RC500USB_writeval(byte addr, byte value);
        [DllImport("RC500USB.dll")]
        public static extern byte RC500USB_readval(byte addr, ref string var);
        [DllImport("RC500USB.dll")]
        public static extern byte RC500USB_value(byte mode, byte addr, ref int value, byte trans_addr);
        [DllImport("RC500USB.dll")]
        public static extern byte RC500USB_load_key(byte mode, byte sexnr, byte key);
        [DllImport("RC500USB.dll")]
        public static extern byte RC500USB_reset(byte msec);
        [DllImport("RC500USB.dll")]
        public static extern byte RC500USB_close();
        [DllImport("RC500USB.dll")]
        public static extern byte RC500USB_config();
        [DllImport("RC500USB.dll")]
        public static extern byte RC500USB_get_info(byte info);
        [DllImport("RC500USB.dll")]
        public static extern byte RC500USB_check_write(byte snr, byte authenmode, byte addr, byte data);
        [DllImport("RC500USB.dll")]
        public static extern byte RC500USB_set_control_bit();
        [DllImport("RC500USB.dll")]
        public static extern byte RC500USB_clr_control_bit();
        [DllImport("RC500USB.dll")]
        public static extern byte RC500USB_read_E2(byte addr, byte length, byte data);
        [DllImport("RC500USB.dll")]
        public static extern byte RC500USB_write_E2(byte addr, byte length, byte data);
        [DllImport("RC500USB.dll")]
        public static extern byte RC500USB_authshc1102(byte keyblock, ref byte[] key);
        [DllImport("RC500USB.dll")]
        public static extern byte RC500USB_readshc1102(byte block, byte[] data);
        [DllImport("RC500USB.dll")]
        public static extern byte RC500USB_writeshc1102(byte block, byte[] data);
        //************************************************************
        //                   函数返回值定义:
        //       0: 操作成功;
        //       1: 在有效区域内没有卡;(Check Write出错)
        //       2: 从卡中接收到了错误的CRC校验和;(Check Write:写出错（比较出错）)
        //       3: 值溢出;
        //       4: 不能验证;
        //       5: 从卡中接收到了错误的校验位;
        //       6: 通信错误;
        //       8: 在防冲突时读到了错误的串行码;
        //       10: 卡没有验证;
        //       11: 从卡中接收到了错误数量的位;
        //       12: 从卡中接收了错误数量的字节;
        //       14: 调用Transfer函数出错;
        //       15: 调用Write函数出错。
        //       16: 调用Increment函数出错
        //       17: 调用Decrment函数出错
        //       18: 调用Read函数出错
        //       24: 冲突错
        //       30: 上一次了送命令时被打断
        //       255:串行通信错误
        //************************************************************
        public static string CheckErr(byte st)
        {
            switch (st)
            {
                case 1:
                    return "对不起，没有检测到卡。";
                case 2:
                    return "对不起，从卡中接收到了错误的CRC校验和;(Check Write:写出错（比较出错）。";
                case 3:
                    return "对不起，值溢出。";
                case 4:
                    return "对不起，不能验证。";
                case 5:
                    return "对不起，从卡中接收到了错误的校验位。";
                case 6:
                    return "对不起，通信错误。";

                case 8:
                    return "对不起，在防冲突时读到了错误的串行码。";

                case 10:
                    return "对不起，卡没有验证。";
                case 11:
                    return "对不起，从卡中接收到了错误数量的位。";
                case 12:
                    return "对不起，从卡中接收了错误数量的字节。";

                case 14:
                    return "对不起，调用Transfer函数出错。";

                case 15:
                    return "对不起，调用Write函数出错。";

                case 16:
                    return "对不起，调用Increment函数出错。";

                case 17:
                    return "对不起，调用Decrment函数出错。";

                case 18:
                    return "对不起，调用Read函数出错。";

                case 24:
                    return "对不起，冲突错。";

                case 30:
                    return "对不起，上一次了送命令时被打断。";
                case 255:
                    return "对不起，串行通信错误。";


                default:
                    return "对不起，未知的错误！";
            }
        }
        /// <summary>
        /// 作用：将字符串内容转化为16进制数据编码，其逆过程是Decode
        /// </summary>
        /// <param name="hexString"></param>
        /// <returns></returns>
        public static byte[] strToToHexByte(string hexString)
        {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            return returnBytes;
        }
        /// <summary>
        ///作用：将16进制数据编码转化为字符
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string byteToHexStr(byte[] bytes)
        {
            string returnStr = "";
            if (bytes != null)
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    returnStr += bytes[i].ToString("X2");
                }
            }
            return returnStr;
        }


        public static string ToHight(int cadid)
        {
            try
            {
                string value;
                byte[] bt = BitConverter.GetBytes(cadid);
                //Array.Reverse(bt);
                value = Convert.ToInt32(byteToHexStr(bt),16).ToString();
                return value;
            }
            catch
            {
                return "Error:数据类型转换异常";
            }
        }

    }
}